<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时不可以修改本文件，影响升级覆盖
 */

class Tiezi extends \Phpcmf\Home\Module
{

	public function index() {
		//get 参数
		$id = (int)\Phpcmf\Service::L('input')->get('id');
		$cid = (int)\Phpcmf\Service::L('input')->get('cid');
		if(!$cid){
		    $this->_json('0', '圈子ID无效');
		}
		if(!$id){
		    $this->_json('0', '帖子ID无效');
		}

		$shuju_data = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('id', $id)->where('cid', $cid)->get()->getRowArray();
		if(!$shuju_data){
		    $this->_json('0', '帖子数据无效');
		}
		$quan_data = \Phpcmf\Service::M()->db->table('1_quan')->where('id', $cid)->where('fstatus', 1)->get()->getRowArray();
		
		// 内容图片列表
		$tupian_data = dr_get_content_img($shuju_data['neirong']);
		$tupian_list = [];
		foreach ($tupian_data as $k => $v) {
			// 去除&quot;及前面的内容
			$v = preg_replace('/.*&quot;/', '', $v);
			$tupian_list[] = dr_get_file($v);
			
		}
		// 头像
		if(dr_avatar($shuju_data['uid'])){
			$touxiang = dr_avatar($shuju_data['uid']);
		} else {
			$touxiang = 1;
		}
		// 九个图 
		$jiugetu_list_0 = dr_string2array($shuju_data['jiugetu']);
		$jiugetu_list = [];
		foreach ($jiugetu_list_0['file'] as $k => $v) {
			if ($k <= 9) {
				$jiugetu_list[] = dr_get_file($v);
			}
		}
		//判断图片判断是否有图片
		if($tupian_list){
			$tupian = [];
		} else {
			$tupian = $jiugetu_list;
		}
		// 音频
		$yinpin_list_0 = dr_string2array($shuju_data['yinpin']);
		$yinpin_list = [];
		foreach ($yinpin_list_0['file'] as $k => $v) {
			if ($k <= 9) {
				$yinpin_list[] = dr_get_file($v);
			}
		}
		// 附件
		$fujian_list_0 = dr_string2array($shuju_data['wenjian']);
		$fujian_list = [];
		// $fujian_list['data'][] = $fujian_list_0['title'];
		
		foreach ($fujian_list_0['file'] as $k => $v) {
			if ($k <= 9) {
				$fujian_list[$k]['url'] = dr_get_file($v);
				$fujian_list[$k]['title'] = $fujian_list_0['title'][$k];
				$fujian_list[$k]['id'] = $v;
			}
		}
		// 视频
		$shipin_data = dr_string2array($shuju_data['shipin']);
		$video_extensions = ['.mp4', '.mov', '.m4v', '.rm', '.rmvb', '.3gp', '.avi', '.flv', '.webm', '.mkv'];
		$shipin_list = [];
		foreach ($shipin_data['file'] as $k => $v) {
		     $shipintu_list = str_ireplace($video_extensions, '', dr_get_file($v));
			if ($k <= 8) {
				$shipin_list[] = [
				        'shipin' => dr_get_file($v),
					    'shipintu' => $shipintu_list,
				    ];
			}
		}
		// 名称
		$userweixin = \Phpcmf\Service::M()->db->table('member_oauth')->where('uid', $shuju_data['uid'])->get()->getRowArray();
		$user = dr_member_info($shuju_data['uid']);
		if($userweixin){
			$mingcheng = $userweixin['nickname'];
		} else if($user['name']) {
			$mingcheng = $user['name'];
		} else {
			$mingcheng = $user['username'];
		}
		// 判断是否购买过 cid 是圈子id
		$yigou_data =  \Phpcmf\Service::M()->db->table('1_quan_buy')->where('uid', $this->uid)->where('cid', $cid)->countAllResults();
		if($yigou_data){
			$yigou = 1;
		} else {
			$yigou = 0;
		}
		if($this->uid == (int)$quan_data['uid'] || $this->member['is_admin']){
			$neirong_data = htmlspecialchars_decode($shuju_data['neirong']);
			$yigou_zhi = 1;
		} else {
			// 判断圈子是否为付费
			if($quan_data['jiage'] > 0){
				if($yigou == 1){
					$neirong_data = htmlspecialchars_decode($shuju_data['neirong']);
					$yigou_zhi = 1;
				} else {
					$neirong_data = '付费圈子，请加入圈子后阅读';
					$yigou_zhi = 0;
				}
			} else {
				$neirong_data = htmlspecialchars_decode($shuju_data['neirong']);
				$yigou_zhi = 1;
			}
		}
		//判断是否加入圈子
		$jiaquan_data =  \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('uid', $this->uid)->where('quanid', $cid)->countAllResults();
		if($jiaquan_data){
		    $jiaru = 1; //已加圈
		} else {
		    $jiaru = 0; //未加圈
		}
		//圈子设置
		$quanzhi_shezhi_0 = \Phpcmf\Service::M()->table('1_quan_shezhi')->where('dirname', 'quan')->getRow();
		$quanzhi_shezhi = dr_string2array($quanzhi_shezhi_0['setting']);
		//更新浏览量
		// 相同的ip 不增加
		// 10小时后才能再次增加浏览量
		$ip = \Phpcmf\Service::L('input')->ip_info();
		$cache_key = 'tiezi_hits_' . $id . '_' . $ip;
		// 10小时 = 36000秒
		if (!\Phpcmf\Service::L('cache')->get_data('quan_tiezi_hits_' . $id.'_'.$ip)) {
			$hits = $shuju_data['hits'] + 1;
			\Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('id', $id)->update(['hits' => $hits]);
			// 设置缓存，过期时间10小时
			//\Phpcmf\Service::L('cache')->set_data("缓存名称", "缓存变量值，支持数组", "缓存有效期，单位秒");
			\Phpcmf\Service::L('cache')->set_data("quan_tiezi_hits_".$id.'_'.$ip,"ip,1","36000");
		}
		// 进度
		$jindu_index_0 = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->where('quanid', $cid)->countAllResults();
		if($jindu_index_0){
			$jindu = 1;
			$jindu_index_1 = \Phpcmf\Service::M()->db->table('1_quan_form_jindu_index')->where('tieid', $id)->where('quanid', $cid)->get()->getRowArray();
			// 转换成数组
			$jindu_index = dr_string2array($jindu_index_1);
			$jindu_index['tianjiashijian'] = dr_date($jindu_index['tianjiashijian'], 'm-d-H:i');
			$jindu_index['kaishishijian'] = dr_date($jindu_index['kaishishijian'], 'm-d-H:i');
			$jindu_index['jieshushijian'] = dr_date($jindu_index['jieshushijian'], 'm-d-H:i');
			$jindu_data = \Phpcmf\Service::M()->table('1_quan_form_jindu_data')->where('tieid', $id)->where('quanid', $cid)->getAll();
		    $jindu_id = $jindu_index['id'];
		} else {
			$jindu = 0;
			$jindu_index = [];
			$jindu_data = [];
			$jindu_id = 0;
		}
		//贴数
		$tieshu = \Phpcmf\Service::M()->db->table('1_quan_form_quantie')->where('cid', $cid)->where('status', 1)->countAllResults();
		//圈子会员数量
		$huiyuan = \Phpcmf\Service::M()->db->table('1_quan_jiaquan')->where('quanid', $cid)->countAllResults();
		
		if($shuju_data['status']==0){
			// 审核中
			if($this->member['is_admin']){
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian,
					'tupian_kaiguan' => $shuju_data['tpkg'],
					//'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => $neirong_data,
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'], // 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'jindu' => $jindu,
					'jindu_id' => $jindu_id,
					'jindu_index' => $jindu_index,
					'jindu_data' => $jindu_data,
					'yigou' => $yigou,
					'quan_jiage' => $quan_data['jiage'],
					'shipin' => $shipin_list,
					'shipin_kaiguan' => $shuju_data['spkg'],//视频开关
					'yinpin' => $yinpin_list, // 音频数据
					'quan_id' => $quan_data['id'], //圈id
					'quan_uid' => $quan_data['uid'], //圈uid
					'quan_ming' => $quan_data['title'], //圈名称
					'quan_jiaru' => $jiaru, // 是否加入圈子，0没有。1加入
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
					'quan_xinxi' => $quanzhi_shezhi['pages']['quan_xinxi'],
					'quan_fuzhi' => $quanzhi_shezhi['pages']['quan_fuzhi'],
					'fujian' => $fujian_list,//附件下载
					'fujian_kaiguan' => $shuju_data['fjkg'],//附件开关
				];

			} else {
				// 审核中
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian,
					'tupian_kaiguan' => $shuju_data['tpkg'],
					//'jiugetu' => $jiugetu_list,
					'jianjie' => '',
					'neirong_data' => '审核中...',
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'], // 浏览量
					'ip' => $ip,
					// 使用member数组中的uid，确保有值
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'jindu' => $jindu, //进度状态
					'jindu_id' => $jindu_id, //进度id
					'jindu_index' => $jindu_index, //进度数据
					'jindu_data' => $jindu_data, //进度细节数据
					'yigou' => $yigou,
					'quan_jiage' => $quan_data['jiage'],
					'shipin' => $shipin_list, //视频
					'shipin_kaiguan' => $shuju_data['spkg'],//视频开关
					'yinpin' => $yinpin_list, // 音频数据
					'quan_id' => $quan_data['id'], //圈id
					'quan_uid' => $quan_data['uid'], //圈uid
					'quan_ming' => $quan_data['title'], //圈名称
					'quan_jiaru' => $jiaru, // 是否加入圈子，0没有。1加入
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
					'quan_xinxi' => $quanzhi_shezhi['pages']['quan_xinxi'],
					'quan_fuzhi' => $quanzhi_shezhi['pages']['quan_fuzhi'],//复制：0复制，1不能复制
					'fujian' => $fujian_list,//附件下载
					'fujian_kaiguan' => $shuju_data['fjkg'],//附件开关
				];
			}

		} else {
			if($shuju_data['gongkai']==1){
				// 公开
				$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian,
					'tupian_kaiguan' => $shuju_data['tpkg'],
					//'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => $neirong_data,
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'jindu' => $jindu, //进度状态
					'jindu_id' => $jindu_id, //进度id
					'jindu_index' => $jindu_index, //进度数据
					'jindu_data' => $jindu_data, //进度细节数据
					'yigou' => $yigou,
					'quan_jiage' => $quan_data['jiage'],
					'shipin' => $shipin_list,
					'shipin_kaiguan' => $shuju_data['spkg'],//视频开关
					'yinpin' => $yinpin_list, // 音频数据
					'quan_id' => $quan_data['id'], //圈id
					'quan_uid' => $quan_data['uid'], //圈uid
					'quan_ming' => $quan_data['title'], //圈名称
					'quan_jiaru' => $jiaru, // 是否加入圈子，0没有。1加入
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
					'quan_xinxi' => $quanzhi_shezhi['pages']['quan_xinxi'],
					'quan_fuzhi' => $quanzhi_shezhi['pages']['quan_fuzhi'],//复制：0复制，1不能复制
					'fujian' => $fujian_list,//附件下载
					'fujian_kaiguan' => $shuju_data['fjkg'],//附件开关
				];
			} else {
				// 隐私
				if($this->member['uid'] == $shuju_data['uid']){
					$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => $tupian,
					'tupian_kaiguan' => $shuju_data['tpkg'],
					//'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => $neirong_data,
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'jindu' => $jindu, //进度状态
					'jindu_id' => $jindu_id, //进度id
					'jindu_index' => $jindu_index, //进度数据
					'jindu_data' => $jindu_data, //进度细节数据
					'yigou' => $yigou,
					'quan_jiage' => $quan_data['jiage'],
					'shipin' => $shipin_list,
					'shipin_kaiguan' => $shuju_data['spkg'],//视频开关
					'yinpin' => $yinpin_list, // 音频数据
					'quan_id' => $quan_data['id'], //圈id
					'quan_uid' => $quan_data['uid'], //圈uid
					'quan_ming' => $quan_data['title'], //圈名称
					'quan_jiaru' => $jiaru, // 是否加入圈子，0没有。1加入
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
					'quan_xinxi' => $quanzhi_shezhi['pages']['quan_xinxi'],
					'quan_fuzhi' => $quanzhi_shezhi['pages']['quan_fuzhi'],//复制：0复制，1不能复制
					'fujian' => $fujian_list,//附件下载
					'fujian_kaiguan' => $shuju_data['fjkg'],//附件开关
				];
				} else {
					$list_data[] = [
					'id' => $shuju_data['id'],
					'uid' => $shuju_data['uid'],
					'touxiang' => $touxiang,
					'mingcheng' => $mingcheng,
					'title' => $shuju_data['title'],
					'tupian' => [''],
					'tupian_kaiguan' => 0,
					'jiugetu' => $jiugetu_list,
					'jianjie' => dr_strcut(dr_clearhtml($shuju_data['neirong']), 100),
					'neirong_data' => '隐私内容，仅作者可见',
					'fabushijian' => dr_date($shuju_data['inputtime'], 'Y-m-d H:i:s'),
					'quanzi' => $quan_data['title'],
					'gongkai' => $shuju_data['gongkai'],
					'hits' => $shuju_data['hits'],// 浏览量
					'ip' => $ip,
					'uid_admin' => $this->member,
					'gongkai' => $shuju_data['gongkai'],
					'status' => $shuju_data['status'],
					'jindu' => $jindu, //进度状态
					'jindu_id' => $jindu_id, //进度id
					'jindu_index' => $jindu_index, //进度数据
					'jindu_data' => $jindu_data, //进度细节数据
					'yigou' => $yigou,
					'quan_jiage' => $quan_data['jiage'],
					'shipin' => '',
					'shipin_kaiguan' => '',//视频开关
					'yinpin' => '', // 音频数据
					'quan_id' => $quan_data['id'], //圈id
					'quan_uid' => $quan_data['uid'], //圈uid
					'quan_ming' => $quan_data['title'], //圈名称
					'quan_jiaru' => $jiaru, // 是否加入圈子，0没有。1加入
					'quan_tubiao' => dr_get_file($quan_data['thumb']),
					'quan_tieshu' => $tieshu, //圈子帖子数量
					'quan_huiyuan' => $huiyuan, //圈子会员数量
					'quan_xinxi' => $quanzhi_shezhi['pages']['quan_xinxi'],
					'quan_fuzhi' => $quanzhi_shezhi['pages']['quan_fuzhi'],//复制：0复制，1不能复制
					'fujian' => $fujian_list,//附件下载
					'fujian_kaiguan' => $shuju_data['fjkg'],//附件开关
				];
				}
			}
		}
	
		$this->_json('1', '读取成功', $list_data);
	}

}
